//
package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.Com.Category;
import com.itheima.common.Com.Dish;
import com.itheima.common.Com.Flavors;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.vo.DishDto;
import com.itheima.common.vo.DishVo;
import com.itheima.common.pojo.DishVo2;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public List<DishVo> getList(String categoryId) {
        //todo 分类名没有查到

        List<DishVo> list = dishMapper.getList(categoryId);

        //用分类id去分类表获取name
//        for (DishVo dishVo : list) {
//
//            Category byId = categoryMapper.getById(categoryId);
//
//            dishVo.setCategoryName(byId.getName());
//        }


        return list;
    }

    /**
     * 添加数据
     *
     * @param dishVo dish的封装类
     * @return 改动的行数
     * 在事务后面加rollbackFor=运行时异常 就是代表遇到这个异常或者他的子类时需要回滚数据
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Boolean save(DishVo dishVo) {

        //把 dish new 出来
        Dish dish = new Dish();

        //调用工具类 复制数据 注意复制数据会把新建的对象的数据给覆盖掉
        BeanUtils.copyProperties(dishVo, dish);
        //雪花id
        dish.setId(IdUtil.getSnowflakeNextIdStr());

//        Category category=new Category();
//        String id = category.getId();
//        dish.setCategoryId(id);
        //改动的行数
        int save = dishMapper.save(dish);
        //获得一个新的集合用来装数据
        List<Flavors> list = dishVo.getFlavors();

        //循环遍历集合，把数据遍历出来进行替换
        for (Flavors flavors : list) {

            String snowflakeNextIdStr = IdUtil.getSnowflakeNextIdStr();

            flavors.setId(snowflakeNextIdStr);

            flavors.setDishId(dish.getId());
            dishMapper.saveFalors(flavors);

        }

        return save == 1;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Boolean update(DishDto dishDto) {
        if (dishDto == null) {
            throw new RuntimeException("参数不能为空");
        }
        //获取数据库里的数据

        //将前端的数据换到数据库里
        //对象
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDto, dish);
        //将数据传回mapper
        int update = dishMapper.update(dish);
        String id = dishDto.getId();
        dishMapper.deletebyid(id);
        List<Flavors> flavors = dishDto.getFlavors();
        for (Flavors flavor : flavors) {
            flavor.setId(IdUtil.getSnowflakeNextIdStr());
            flavor.setDishId(dishDto.getId());
            dishMapper.addflavors(flavor);
        }


        if (update == 0) {
            throw new RuntimeException("更新菜品失败");
        }
        return update > 0;
    }

    @Override
    public Boolean delete(String ids) {

        //转换成数组
        String[] arr = ids.split(",");
        int delete = dishMapper.delete(arr);
        return delete > 0;
    }

    @Override
    public void discontinued(Integer status, String ids) {
        //split是用来切割数据的，将数据切割放进String类型的数组
        String[] id = ids.split(",");

        dishMapper.discontinued(status, id);
    }

    @Override
    public PageInfo<Dish> getpage(String name, Integer page, Integer pageSize, Integer type) {

        PageInfo<Dish> pageInfo = new PageInfo<Dish>();

        Integer start = (page - 1) * pageSize;

        List<Dish> pa = dishMapper.getpage(name, start, pageSize, type);
        for (Dish dish : pa) {
            log.info("{}", dish);
            Category byId = categoryMapper.findbyId(dish.getCategoryId());

            dish.setCategoryName(byId.getName());
        }

        pageInfo.setRecords(pa);

        pageInfo.setSize(pageSize);

        pageInfo.setCurrent(page);

        long count = dishMapper.count(name);

        pageInfo.setTotal(count);

        return pageInfo;
    }

    @Override
    public DishVo getById(String id) {
        //根据id查询
        Dish findbyid = dishMapper.findbyid(id);
//        String categoryId = findbyid.getCategoryId();
        DishVo dishvo = new DishVo();
        //复制
        BeanUtils.copyProperties(findbyid, dishvo);
        //根据id获取集合
        List<Flavors> flavors = dishMapper.getByidFlavors(findbyid.getId());

        dishvo.setFlavors(flavors);

        return dishvo;
    }

    @Override
    public List<DishVo2> getAll(String categoryId, Integer status, Integer page, Integer pageSize) {
        //获得一个对象
//        List<DishVo2> list=new ArrayList<>();
        DishVo2 dishVo2=new DishVo2();
        //创建一个start封装page
        Integer start = (page - 1) * pageSize;
        //通过SQL语句查询所需要的数据
        List<Dish> all = dishMapper.getAll(categoryId, status, start, pageSize);
        for (Dish vo2 : all) {
            log.info("{}", vo2);
            Category category = categoryMapper.findbyId(categoryId);
            vo2.setCategoryName(category.getName());

            List<Flavors> byidFlavors = dishMapper.getByidFlavors(vo2.getId());
            log.info("{}",byidFlavors);
            dishVo2.setFlavors(byidFlavors);
            dishVo2.setDescription(vo2.getDescription());
            dishVo2.setImage(vo2.getImage());
            dishVo2.setName(vo2.getName());
            dishVo2.setPrice(vo2.getPrice());
            dishVo2.setStatus(status);
            dishVo2.setId(vo2.getId());

        }
        List<DishVo2> dishVo2s=new ArrayList<>();
dishVo2s.add(dishVo2);
        dishVo2.setCategoryId(categoryId);




        return dishVo2s;
    }


}
